package patrol.mobile.com.mobilepatrol.widget;

import android.annotation.SuppressLint;
import android.content.Context;
import android.database.DataSetObserver;
import android.graphics.drawable.ColorDrawable;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.ListPopupWindow;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.Spinner;
import android.widget.SpinnerAdapter;
import android.widget.TextView;

import java.util.List;

import patrol.mobile.com.mobilepatrol.R;

/**
 * Created by z on 2018/9/9.
 * Des: 自定义Spanner
 */

public class MySpinner extends LinearLayout {
    private View mView;
    private List<String> mDatas;
    private ListPopupWindow listPopupWindow;
    private TextView mText, mCenterText;
    private RelativeLayout mRelativeLayout;
    private ImageView mHitImage;

    private CallBack callBack;

    public TextView getmText() {
        return mText;
    }

    public TextView getmCenterText() {
        return mCenterText;
    }

    public RelativeLayout getmRelativeLayout() {
        return mRelativeLayout;
    }

    public ImageView getmHitImage() {
        return mHitImage;
    }

    public void setmDatas(List<String> mDatas) {
        this.mDatas = mDatas;
    }

    public void setmDatas(List<String> mDatas, CallBack callBack) {
        this.mDatas = mDatas;
        this.callBack = callBack;
        mText.setText(mDatas.get(0));
        mCenterText.setText(mDatas.get(0));
    }

    public void setOnItemSelectedListener() {
        if (listPopupWindow != null && listPopupWindow.isShowing()) {
            listPopupWindow.dismiss();
        } else {
            listPopupWindow = new ListPopupWindow(getContext());
            listPopupWindow.setWidth(this.getWidth());//设置宽度
            listPopupWindow.setHeight(ListPopupWindow.WRAP_CONTENT);//设置高度
            listPopupWindow.setBackgroundDrawable(new ColorDrawable(ContextCompat.getColor(getContext(), R.color.white)));//设置背景色
            listPopupWindow.setAdapter(new MySpinnerAdapter());
            listPopupWindow.setAnchorView(this);
            listPopupWindow.show();
        }
    }

    public MySpinner(Context context) {
        super(context);
        initView();
    }

    public void setText(String str) {
        mText.setText(str);
    }

    public MySpinner(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initView();
    }

    public MySpinner(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView();
    }

    private void initView() {
        mView = LayoutInflater.from(getContext()).inflate(R.layout.view_mspanner_layout, null);
        mView.setLayoutParams(new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        mText = mView.findViewById(R.id.mText);
        mCenterText = mView.findViewById(R.id.mCenterText);
        mRelativeLayout = mView.findViewById(R.id.mRelativeLayout);
        mHitImage = mView.findViewById(R.id.mHitImage);
        addView(mView);
        setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listPopupWindow != null && listPopupWindow.isShowing()) {
                    listPopupWindow.dismiss();
                    return;
                }
                listPopupWindow = new ListPopupWindow(getContext());
                listPopupWindow.setWidth(MySpinner.this.getWidth());//设置宽度
                listPopupWindow.setHeight(ListPopupWindow.WRAP_CONTENT);//设置高度
                listPopupWindow.setBackgroundDrawable(new ColorDrawable(ContextCompat.getColor(getContext(), R.color.white)));//设置背景色
                listPopupWindow.setAdapter(new MySpinnerAdapter());
                listPopupWindow.setAnchorView(MySpinner.this);
                listPopupWindow.show();

            }
        });
    }

    public class MySpinnerAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return mDatas == null ? 0 : mDatas.size();
        }

        @Override
        public Object getItem(int position) {
            return mDatas.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @SuppressLint("ViewHolder")
        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            convertView = LayoutInflater.from(getContext()).inflate(R.layout.item_spinner_layout, null);
            TextView textView = convertView.findViewById(R.id.mText);
            textView.setText(mDatas.get(position));
            convertView.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    callBack.onSelect(position);
                    listPopupWindow.dismiss();
                    mCenterText.setText(mDatas.get(position));
                    mText.setText(mDatas.get(position));
                }
            });
            return convertView;

        }
    }


    public interface CallBack {
        void onSelect(int position);
    }

}
